package fti.uksw.ruang.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

//import android.app.ActionBar;
import android.app.Activity;
//import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import fti.uksw.ruang.LoginActivity;
import fti.uksw.ruang.R;
import fti.uksw.ruang.dialog.YesNoDialog;
import fti.uksw.ruang.model.CalendarList;
import fti.uksw.ruang.model.MsgCode;
import fti.uksw.ruang.websocket.WebSocketClient;

public class AddRoomFragment extends Fragment {

	public static final String TAG = AddRoomFragment.class.getName();

	private ListView lvRoom;
	private EditText etRoom;

	private AdapterRoomFragment adapter;

	private AddRoomFragmentListener mListener;
	private ArrayList<CalendarList> listCalList;

	private WebSocketClient wsClient = LoginActivity.wsClient;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// listCalList = getActivity().getIntent().getParcelableArrayListExtra(
		// CalendarList.TAG);
//		adapter = new AdapterRoomFragment(getActivity(), listCalList);

		// ActionBar ab = getActivity().getActionBar();
		// ab.setTitle("Add Room");

	}

	public void setRooms(ArrayList<CalendarList> calList) {
		adapter = new AdapterRoomFragment(getActivity(), calList);
		lvRoom.setAdapter(adapter);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.newroom, container, false);

		lvRoom = (ListView) v.findViewById(R.id.lv_newroom_listroom);
		etRoom = (EditText) v.findViewById(R.id.et_newroom_roomname);
		etRoom.addTextChangedListener(etRoomTextWatcher);

		lvRoom.setOnItemClickListener(listRoomClick);

		return v;
	}

	private OnItemClickListener listRoomClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> mAdapter, View v, final int pos,
				long id) {
			YesNoDialog yesNoDialog = YesNoDialog
					.newInstance(
							getActivity().getApplicationContext(),
							"Delete Room",
							"Delete room will delete all event related to this room. Are you sure wanna delete this room? ");
			yesNoDialog
					.setYesNoDialogListener(new YesNoDialog.YesNoDialogListener() {

						@Override
						public void yesListener() {
							CalendarList _cl = (CalendarList) adapter
									.getItem(pos);
							JSONObject msgReq = new JSONObject();
							try {
								msgReq.put(MsgCode.MSGCODE,
										MsgCode.DELCALENDARLISTREQ);
								msgReq.put(CalendarList.ID, _cl.getId());
								msgReq.put(CalendarList.CALENDARID, _cl.getId());
								wsClient.sendTextMessage(msgReq.toString());
								adapter.deleteItem(_cl);
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}

						@Override
						public void noListener() {
						}
					});
			yesNoDialog.show(getFragmentManager(), "yesnodialog");
		}
	};

	private TextWatcher etRoomTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			CalendarList calList = new CalendarList();
			calList.setCalendarTitle(etRoom.getText().toString());
			calList.setCalendarId(etRoom.getText().toString());
			calList.setJenis("");
			calList.setKapasitas(0);
			mListener.itemRoom(calList);
		}
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (AddRoomFragmentListener) activity;
		} catch (ClassCastException cce) {
			throw new ClassCastException(activity.toString()
					+ " must implemented AddRoomFragmentListener");
		}
	}

	public interface AddRoomFragmentListener {
		public void itemRoom(CalendarList mCalList);
	}

	public class AdapterRoomFragment extends BaseAdapter {

		private Activity activity;
		private LayoutInflater inflater;
		private List<CalendarList> list;

		public AdapterRoomFragment(Activity a, List<CalendarList> listRuang) {
			activity = a;
			inflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			list = listRuang;
		}

		public void deleteItem(CalendarList cl) {
			list.remove(cl);
			adapter.notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public CalendarList getItem(int pos) {
			return list.get(pos);
		}

		@Override
		public long getItemId(int position) {
			return list.get(position).getId();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.room_listview, null);
			CalendarList cl = list.get(position);

			TextView tvRoomName = (TextView) convertView
					.findViewById(R.id.tv_newroomlv_roomname);
			tvRoomName.setText(cl.getCalendarTitle());
			return convertView;
		}

	}

}
